package com.spzx.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.user.api.domain.UserBrowseHistory;
import com.spzx.user.mapper.UserBrowseHistoryMapper;
import com.spzx.user.service.IUserBrowseHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 用户浏览记录表 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2024-11-18
 */
@Service
public class UserBrowseHistoryServiceImpl extends ServiceImpl<UserBrowseHistoryMapper, UserBrowseHistory> implements IUserBrowseHistoryService {

    @Autowired
    private UserBrowseHistoryMapper userBrowseHistoryMapper;


    /**
     * 保存用户浏览历史
     * @param userBrowseHistory 用户浏览历史对象
     */
    @Override
    public void saveUserBrowseHistory(UserBrowseHistory userBrowseHistory) {
        //根据用户id和skuId查询数据库中是否已经有该记录
        UserBrowseHistory userBrowseHistoryDatabase = userBrowseHistoryMapper.selectOne(
                new LambdaQueryWrapper<UserBrowseHistory>()
                        .eq(UserBrowseHistory::getUserId, userBrowseHistory.getUserId())
                        .eq(UserBrowseHistory::getSkuId, userBrowseHistory.getSkuId()));
        //验空
        if (userBrowseHistoryDatabase != null) {
            //新浏览记录的更新时间设置为当前时间
            UserBrowseHistory userBrowseHistoryNew = new UserBrowseHistory();
            //设置主键
            Long id = userBrowseHistoryDatabase.getId();
            userBrowseHistoryNew.setId(id);
            //设置更新时间
            userBrowseHistoryNew.setUpdateTime(new Date());
            //调用根据id更新的方法
            baseMapper.updateById(userBrowseHistoryNew);
        } else {
            //调用添加的方法
            baseMapper.insert(userBrowseHistory);
        }
    }
}
